package com.weilele.leisure.time.base

import androidx.lifecycle.MutableLiveData
import com.weilele.base.library.BaseViewModel
import com.weilele.leisure.time.bean.BaseResp
import com.weilele.mvvm.base.livedata.StatusData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext

open class BaseVideoViewModel : BaseViewModel() {
    /**
     * 访问网络请求
     */
    fun <T> MutableLiveData<StatusData<T>>.requestVideoApi(
        context: CoroutineContext = Dispatchers.Main,
        call: suspend () -> BaseResp<T>
    ): Job? {
        return this.runInCoroutineScope(
            context = context,
            runIfSuccess = true
        ) {
            val result = call.invoke()
            if (result.status == 200) {
                result.result
            } else {
                throw IllegalArgumentException(result.msg)
            }
        }
    }
}